#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#include "sconf.h"

const char *file_str =
"abcd, 123, -1024, true, false,\n"
"line1, date, 2021, data, line one,\n"
"line2, date, 2021, data, line two,\n";

int main(int argc, char *argv[])
{
    printf("sconf test.\n");
    char *file_buf;
    char *p;
    FILE *in_fp = fopen("config", "r");
    if (in_fp) {
        fseek(in_fp, 0, SEEK_END);
        long size = ftell(in_fp);
        fseek(in_fp, 0, SEEK_SET);
        char *file_buf = malloc(size + 1);
        fread(file_buf, size, 1, in_fp);
        printf("buf:%s\n", file_buf);
        fclose(in_fp);
        p = (char *) file_buf;
    } else {
        p = (char *) file_str;
    }

    FILE *out_fp = fopen("config", "w");

    while (1) {
        char line[128] = {0};
        p = sconf_readline(p, line, 128);
        if (!p)
            break;
        //printf("read line: %s\n", line);
        char new_line[128] = {0};
        char *q = line;
        while (1) {
            char buf[32] = {0};
            q = sconf_read(q, buf, 32);
            if (!q)
                break;
            //printf("read: %s\n", buf);
            printf("item: %s\n", sconf_trim(buf));
            //printf("int: %d\n", sconf_int(buf));
            //printf("bool: %d\n", sconf_bool(buf));

            sconf_write(new_line, buf);
        }
        sconf_writeline(new_line);
        //printf("new line: %s\n", new_line);

        fwrite(new_line, strlen(new_line), 1,out_fp);
    }
    fclose(out_fp);
    return 0;
}
